﻿#include "../include/interface.h"

#define _TEST false
#define _ENABLED_CONSOLE_DEBUG true
#define _RUN_PROGRAM_WITHOUT_PARAM true
using namespace std;

#if _TEST
int main() {
	system("chcp 65001>nul");
	int argc = 5;
	char **argv = new char *[argc];
	bool *isDuplicate = new bool(false);
	argv[0] = new char[13];
	argv[1] = new char[13];
	argv[2] = new char[2];
	argv[3] = new char[13];
	argv[4] = new char[2];

	strcpy(argv[0], "program info");
	strcpy(argv[1], "player name1");
	strcpy(argv[2], "+");
	strcpy(argv[3], "player name2");
	strcpy(argv[4], "*");

	// analysis
	for (int i = 1; i < argc; i += 2) {
		char *playerName = argv[i];
		char *styleCode = argv[i + 1];
		for (int j = i + 2; j < argc; j += 2) {
			if (strcmp(argv[j], playerName) == 0) {
				printf("发现重名项：argv[%d], argv[%d] -> {\"%s\", \"%s\"};\n这是否是有意的？\n", i, j, argv[i], argv[j]);
				*isDuplicate = true;
			}
			if (strcmp(argv[j + 1], styleCode) == 0) {
				printf("发现重名项：argv[%d], argv[%d] -> {\"%s\", \"%s\"};\n这是否是有意的？\n", i + 1, j + 1, argv[i + 1], argv[j + 1]);
				*isDuplicate = true;
			}
		}
	}
	if (*isDuplicate) {
		delete isDuplicate;
		clog << "String duplicated!";
		return -1;
	}
	delete isDuplicate;
	for (size_t i = 0; i < 5; i++) {
		delete[] argv[i];
	}
	delete[] argv;
	cout << "initialized successfully!";
	return 0;
}

#elif _ENABLED_CONSOLE_DEBUG

#define CONSTANT_ARGC 1
const int requireArgumentCount = 4;

// 参数1 第1个玩家名
// 参数2 第一个玩家的棋子形状
// 参数...
int main(int argc, char *argv[]) {
#ifdef _MSVC_LANG
	wcout.imbue(locale("CHS"));
	cout.imbue(locale("CHS"));
#endif// _MSVC_LANG

	vector<Player *> players;

	// 游戏逻辑

	unsigned winCount = 3;

	// 分配
	ChessBoard map(winCount, winCount, L'+');

#if !_RUN_PROGRAM_WITHOUT_PARAM
	if (argc == 2 && *argv == "/?" || argc == 1) {
		// help info
		cout << "[玩家名][玩家棋子形状]..." << endl
			 << "这个应用需要至少" << requireArgumentCount << "个参数" << endl;
		return 0;
	}
	if (argc < requireArgumentCount + 1 && argc - CONSTANT_ARGC != 0) {
		cout << "这个程序不能正常运行因为这个程序需要至少"
			 << CONSTANT_ARGC + 1
			 << " 个参数才能运行" << endl
			 << "请输入 "
			 << argv[0] << "/? 获取详情信息" << endl;
		return -1;
	}

	bool *isDuplicate = new bool(false);// analysis
	for (int i = 1; i < argc; i += 2) {
		char *playerName = argv[i];
		char *styleCode = argv[i + 1];
		for (int j = i + 2; j < argc; j += 2) {
			if (strcmp(argv[j], playerName) == 0) {
				printf("发现重名项：argv[%d], argv[%d] -> {\"%s\", \"%s\"};\n这是否是有意的？\n",
					   i, j, argv[i], argv[j]);
				*isDuplicate = true;
			}
			if (strcmp(argv[j + 1], styleCode) == 0) {
				printf("发现重名项：argv[%d], argv[%d] -> {\"%s\", \"%s\"};\n这是否是有意的？\n",
					   i + 1, j + 1, argv[i + 1], argv[j + 1]);
				*isDuplicate = true;
			}
		}
	}
	if (*isDuplicate) {
		cout << "应用程序退出，原因：玩家名或玩家棋子形状重复。" << endl;
		delete isDuplicate;
		return -1;
	}
	delete isDuplicate;

	for (int idx = CONSTANT_ARGC; idx < argc; idx++) {
		if (idx % 2 == 0) {
			players.push_back(new Player(argv[idx], idx - CONSTANT_ARGC));
		} else {
			map.loadAsset(wchar_t(*argv[idx]));
		}
	}
#else
	players.push_back(new Player("sb",1));
	players.push_back(new Player("fuck", 2));
#endif
	return 0;
}
#else
int main() {
	return 0;
}
#endif// MAIN_FOR_TEST
#pragma warning(pop)